package com.czwx.bee.business.manager.modular.cloanuser.controller;

import com.czwx.bee.base.common.dto.ResponseDto;
import com.czwx.bee.base.common.exception.BizExceptionEnum;
import com.czwx.bee.base.web.common.BaseController;
import com.czwx.bee.business.manager.modular.cloanuser.dto.CloanUserDto;
import com.czwx.bee.business.manager.modular.cloanuser.model.query.CloanUserQuery;
import com.czwx.bee.business.manager.modular.cloanuser.service.CloanUserService;
import com.czwx.bee.business.manager.modular.sys.model.SecurityUser;
import com.czwx.bee.business.manager.utils.SecurityUtil;
import io.swagger.annotations.*;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("cloanuser")
@Api(value = "CloanUserController", tags = "分期用户管理")
public class CloanUserController extends BaseController {

    @Autowired
    private CloanUserService cloanUserService;


    @PostMapping("/list")
    @ApiOperation(value = "分期用户列表", notes = "可分页条件查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "cloanUserQuery", value = "请输入查询信息", required = true, dataType = "CloanUserQuery", paramType = "body") })
    @ApiResponses(
            @ApiResponse(code = 200,response = CloanUserDto.class,message = "data：中返回list")
    )
    public ResponseDto list(@RequestBody CloanUserQuery cloanUserQuery) throws Exception {
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        SecurityUser securityUser=SecurityUtil.getSecurityUser();
        Map params=objectToMap(cloanUserQuery);
        params.put("drainageId",securityUser.getDrainageId());
        PageQuery query=  new PageQuery(cloanUserQuery.getPage(),cloanUserQuery.getSize(),params);
        cloanUserService.getPageList(query);
        rs.setData(query);
        return rs;
    }

    @PostMapping("/test")
    @ApiOperation(value = "test", notes = "可分页条件查询", httpMethod = "POST")
    @PreAuthorize("hasAuthority('cluser_list')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "cloanUserQuery", value = "请输入查询信息", required = true, dataType = "CloanUserQuery", paramType = "body") })
    public ResponseDto test(@RequestBody CloanUserQuery cloanUserQuery) throws Exception {
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        cloanUserService.test();
        return rs;
    }

}
